#ifndef STATICDUALLINKLIST_H
#define STATICDUALLINKLIST_H

#include "DualLinkList.h"

namespace DSLib
{

template <typename T, int N>
class StaticDualLinkList : public DualLinkList<T>
{
protected:
	typedef typename DualLinkList<T>::Node Node;

	struct SNode : public Node
	{
		void* operator new (unsigned int size, void* loc)
		{
			(void)size;
			return loc;
		}
	};

	virtual Node* create()
	{
		SNode* ret = NULL;
		for (int i = 0; i < N; i++)
		{
			if (!m_used[i])
			{
				ret = reinterpret_cast<SNode*>(&m_space) + i;
				ret = new(ret)SNode();
				m_used[i] = 1;
				break;
			}
		}
		return ret;
	}

	virtual void destory(Node* pn)
	{
		SNode* space = reinterpret_cast<SNode*>(&m_space);
		SNode* psn = dynamic_cast<SNode*>(pn);

		for (int i = 0; i < N; i++)
		{
			if (space + i == psn)
			{
				m_used[i] = 0;
				psn->~SNode();
				break;
			}
		}
	}
	unsigned char m_space[sizeof(SNode) * N];
	int m_used[N];

public:
	StaticDualLinkList()
	{
		for (int i = 0; i < N; i++)
		{
			m_used[i] = 0;
		}
	}

	int capacity() const
	{
		return N;
	}

	~StaticDualLinkList()
	{
		this->clear();
	}
};
}



#endif